package com.jspgou.cms.action.admin.assist;
import static com.jspgou.common.page.SimplePage.cpn;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.SiteUtils;
import com.jspgou.cms.entity.Message;
import com.jspgou.cms.entity.ReceiverMessage;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.ShopMemberGroup;
import com.jspgou.cms.manager.MessageMng;
import com.jspgou.cms.manager.ReceiverMessageMng;
import com.jspgou.cms.manager.ShopMemberGroupMng;
import com.jspgou.cms.manager.ShopMemberMng;
import com.jspgou.cms.service.LoginSvc;

@Controller
public class MessageAct {
	private static final Logger log = LoggerFactory.getLogger(MessageAct.class);
	
	//打开站内信页面显示
	@RequestMapping("/message/v_list.do")
	public String list(Integer pageNo, String title, Date sendBeginTime,
			Date sendEndTime, Boolean status, Integer box,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		//获取会员名称
		ShopMember member = loginSvc.getMember(request, response, web);
		Pagination pagination = null;
		String returnPage = "message/sendbox";
		if (box == null) {
			box = 1;
		}
		if (box.equals(1)) {
			// 发件箱
			pagination = messageMng.getPage(member.getId(), null,
					title, sendBeginTime, sendEndTime, status, box, false,
					cpn(pageNo), CookieUtils.getPageSize(request));
			returnPage = "message/sendbox";
		} else if (box.equals(3)) {
			// 垃圾箱(可能从收件箱或者从发件箱转过来)
			pagination = receiverMessageMng.getPage(member.getId(),
					member.getId(), title, sendBeginTime, sendEndTime, status,
					box, false, cpn(pageNo), CookieUtils.getPageSize(request));
			returnPage = "message/trashbox";
		}
		model.addAttribute("msg", request.getAttribute("msg"));
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("title", title);
		model.addAttribute("sendBeginTime", sendBeginTime);
		model.addAttribute("sendEndTime", sendEndTime);
		model.addAttribute("status", status);
		model.addAttribute("box", box);
		return returnPage;
	}

	@RequestMapping("/message/v_add.do")
	public String add(ModelMap model) {
		List<ShopMemberGroup> groups = groupMng.getList();
		model.addAttribute("groupList", groups);
		return "message/add";
	}

	// 直接发送
	@RequestMapping("/message/v_send.do")
	public String send(Message message, String username, Long groupId,
			Integer pageNo, String title, Date sendBeginTime, Date sendEndTime,
			Boolean status, Integer box, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		Website web = SiteUtils.getWeb(request);
		//获取会员名称
		ShopMember member = loginSvc.getMember(request, response, web);
		Date now = new Date();
		ReceiverMessage receiverMessage = new ReceiverMessage();
		ShopMember msgReceiverUser=null;
		if(StringUtils.isNotBlank(username)){			
			ShopMember Receiver=shopMemberMng.findUsername(username);
			String receiveruser=Receiver.getRealName();
			msgReceiverUser = shopMemberMng.findByUsername(receiveruser);	
		}
		if (msgReceiverUser != null) {
			messageInfoSet(message, receiverMessage, member, msgReceiverUser,
					now,request);
		}
		// 按会员组推送站内信
		if (groupId != null && groupId!=-1) {
			List<ShopMember> users;
			ShopMember tempUser;
			Message tempMsg;
			ReceiverMessage tempReceiverMsg;
			if (groupId==0) {
				// 所有未禁用会员
				users = shopMemberMng.getList(null, null);
				if (users != null && users.size() > 0) {
					for (int i = 0; i < users.size(); i++) {
						tempUser = users.get(i);
						tempMsg = new Message();
						tempMsg.setMsgTitle(message.getMsgTitle());
						tempMsg.setMsgContent(message.getMsgContent());
						tempReceiverMsg = new ReceiverMessage();
						if (msgReceiverUser != null) {
							if (!tempUser.equals(msgReceiverUser)) {
								messageInfoSet(tempMsg, tempReceiverMsg, member,
										tempUser, now,request);
							}
						} else {
							messageInfoSet(tempMsg, tempReceiverMsg, member,
									tempUser, now, request);
						}
					}
				}
			} else {
				// 非禁用的会员
				users = shopMemberMng.getList(null,groupId);
				if (users != null && users.size() > 0) {
					for (int i = 0; i < users.size(); i++) {
						tempUser = users.get(i);
						tempMsg = new Message();
						tempMsg.setMsgTitle(message.getMsgTitle());
						tempMsg.setMsgContent(message.getMsgContent());
						tempReceiverMsg = new ReceiverMessage();
						if (msgReceiverUser != null) {
							if (!tempUser.equals(msgReceiverUser)) {
								messageInfoSet(tempMsg, tempReceiverMsg, member,
										tempUser, now,request);
							}
						} else {
							messageInfoSet(tempMsg, tempReceiverMsg, member,
									tempUser, now, request);
						}
					}
				}
			}
		}

		return list(pageNo, title, sendBeginTime, sendEndTime, status, 1,
				request, response, model);
	}

	private void messageInfoSet(Message message,
			ReceiverMessage receiverMessage, ShopMember sendUser,
			ShopMember receiverUser, Date sendTime,
			HttpServletRequest request) {
		message.setMsgBox(1);
		message.setMsgSendUser(sendUser);
		message.setMsgReceiverUser(receiverUser);
		message.setMsgStatus(false);
		message.setSendTime(sendTime);
		messageMng.save(message);
		receiverMessage.setMsgBox(0);
		receiverMessage.setMsgContent(message.getMsgContent());
		receiverMessage.setMsgSendUser(sendUser);
		receiverMessage.setMsgReceiverUser(receiverUser);
		receiverMessage.setMsgStatus(false);
		receiverMessage.setMsgTitle(message.getMsgTitle());
		receiverMessage.setSendTime(sendTime);
		receiverMessage.setMessage(message);
		// 接收端（有一定冗余）
		receiverMessageMng.save(receiverMessage);
		log.info("member Message send Message success. id={}", message
				.getId());
	}
	
	//查看信件详情
	@RequestMapping("/message/v_read.do")
	public String read(Integer id, Integer box, HttpServletRequest request,HttpServletResponse response,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = loginSvc.getMember(request, response, web);
		//垃圾箱查看信件
		if(box==3){
			ReceiverMessage message = receiverMessageMng.findById(id);
			if (message != null) {
				// 阅读收信
				// 发件人查看更新已读状态
				if (message.getMsgReceiverUser().equals(member)) {
					message.setMsgStatus(true);
					receiverMessageMng.update(message);
				}
				model.addAttribute("message", message);
			} else {
				// 阅读已发信
				Message msg = messageMng.findById(id);
				model.addAttribute("message", msg);
			}
			model.addAttribute("box", box);
			//收件箱查看信件
		}else if(box==1){
			Message message = messageMng.findById(id);
			if (message != null) {
				// 阅读收信
				// 发件人查看更新已读状态
				if (message.getMsgSendUser().equals(member)) {
					message.setMsgStatus(true);
					messageMng.update(message);
				}
				model.addAttribute("message", message);
			} else {
				// 阅读已发信
				Message msg = messageMng.findById(id);
				model.addAttribute("message", msg);
			}
			model.addAttribute("box", box);
		}
		return "message/read";
	}

	//删除到回收站
	@RequestMapping("/message/v_trash.do")
	public void trash(Integer[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = loginSvc.getMember(request, response, web);
		JSONObject object = new JSONObject();
		Message message;
		ReceiverMessage receiverMessage;
		if (member == null) {
			object.put("result", false);
		} else {
			Iterator<ReceiverMessage> it;
			for (Integer i = 0; i < ids.length; i++) {
				message = messageMng.findById(ids[i]);
				receiverMessage = receiverMessageMng.findById(ids[i]);
				if (message != null && message.getMsgSendUser().equals(member)) {
					// 清空发信表的同时复制该数据到收信表（收信人未空）
					receiverMessage=new ReceiverMessage();
					receiverMessage.setMsgBox(3);
					receiverMessage.setMsgContent(message.getMsgContent());
					receiverMessage.setMsgSendUser(message.getMsgSendUser());
					receiverMessage.setMsgReceiverUser(message.getMsgReceiverUser());
					receiverMessage.setMsgStatus(message.getMsgStatus());
					receiverMessage.setMsgTitle(message.getMsgTitle());
					receiverMessage.setSendTime(message.getSendTime());
					receiverMessage.setMessage(null);
					// 接收端（有一定冗余）
					receiverMessageMng.save(receiverMessage);
					// 清空该发件对应的收件关联关系
					Set<ReceiverMessage> receiverMessages = message
							.getReceiverMsgs();
					ReceiverMessage tempReceiverMessage;
					if (receiverMessages != null && receiverMessages.size() > 0) {
						it = receiverMessages.iterator();
						while (it.hasNext()) {
							tempReceiverMessage= it.next();
							tempReceiverMessage.setMessage(null);
							receiverMessageMng.update(tempReceiverMessage);
						}
					}
					messageMng.deleteById(ids[i]);
				}
				if (receiverMessage != null
						&& receiverMessage.getMsgReceiverUser().equals(member)) {
					receiverMessage.setMsgBox(3);
					receiverMessageMng.update(receiverMessage);
				}
				log.info("member Message trash Message success. id={}",
						ids[i]);
			}
			object.put("result", true);
		}
		ResponseUtils.renderJson(response, object.toString());
	}

	//垃圾箱信息还原
	@RequestMapping("/message/v_revert.do")
	public void revert(Integer ids[], HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = loginSvc.getMember(request, response, web);
		JSONObject object = new JSONObject();
		ReceiverMessage receiverMessage;
		if (member == null) {
			object.put("result", false);
		} else {
			for (Integer i = 0; i < ids.length; i++) {
				receiverMessage = receiverMessageMng.findById(ids[i]);
				//还原发件箱
				if(receiverMessage != null){
					receiverMessage.setMsgBox(1);
					receiverMessageMng.update(receiverMessage);
					if(receiverMessage.getMsgBox()==1){
						Message message=new Message();
						message.setMsgBox(receiverMessage.getMsgBox());
						message.setId(receiverMessage.getId());
						message.setMsgSendUser(receiverMessage.getMsgSendUser());
						message.setMsgReceiverUser(receiverMessage.getMsgReceiverUser());
						message.setMsgStatus(false);
						message.setMsgTitle(receiverMessage.getTitleHtml());
						message.setMsgContent(receiverMessage.getContentHtml());
						message.setSendTime(receiverMessage.getSendTime());
						messageMng.save(message);
					}
					receiverMessageMng.deleteById(ids[i]);
				}
				log.info("member Message revert Message success. id={}",
						ids[i]);
			}
			object.put("result", true);
		}
		ResponseUtils.renderJson(response, object.toString());
	}
	
	//彻底清空
	@RequestMapping("/message/v_empty.do")
	public void empty(Integer ids[], HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = loginSvc.getMember(request, response, web);
		JSONObject object = new JSONObject();
		Message message;
		ReceiverMessage receiverMessage;
		if (member == null) {
			object.put("result", false);
		} else {
			for (Integer i = 0; i < ids.length; i++) {
				// 清空收到的站内信
				receiverMessage = receiverMessageMng.findById(ids[i]);
				//收件箱通过id查找到的信件不为空时，且发信人与登录人一致时才可以删除
				if (receiverMessage != null && receiverMessage.getMsgSendUser().equals(member)) {
					receiverMessageMng.deleteById(ids[i]);
				} else {
					// 清空发送的站内信
					message = receiverMessage.getMessage();
					if (receiverMessage.getMsgBox().equals(3)) {
						// 草稿直接删除
						receiverMessage.setMessage(null);
						if (message != null) {
							messageMng.deleteById(message.getId());
						}
					} else {
						// 非草稿删除和主表的关联
						receiverMessage.setMessage(null);
					}
					if (message != null
							&& message.getMsgSendUser().equals(member)) {
						messageMng.deleteById(message.getId());
					}
				}
				log.info("member Message empty Message success. id={}",
						ids[i]);
			}
			object.put("result", true);
		}
		ResponseUtils.renderJson(response, object.toString());
	}
	
	//查找会员存不存在
	@RequestMapping("/message/v_findUser.do")
	public void findUserByUserName(String username, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {	
		Website web = SiteUtils.getWeb(request);
		JSONObject object = new JSONObject();
		//获取后台用户
		ShopMember member = loginSvc.getMember(request, response, web);
		if (member == null) {
			object.put("result", false);
		} 
		Boolean exist = shopMemberMng.usernameNotExist(username);
		ShopMember user=shopMemberMng.findUsername(username);
		if(user!=null){
			if(member.getUsername().equals(user.getUsername())){
				object.put("result", true);
				object.put("frontuser", user.getUsername());
			} 
		}else{
			if(exist==true){
				object.put("result", true);
				object.put("exist", exist);
			}
		}
		ResponseUtils.renderJson(response, object.toString());
	}
	
	@Autowired
	private LoginSvc loginSvc;
	@Autowired
	private MessageMng messageMng;
	@Autowired
	private ReceiverMessageMng receiverMessageMng;
	@Autowired
	private ShopMemberMng shopMemberMng;
	@Autowired
	private ShopMemberGroupMng groupMng;
}